package yunhe.servlet;

import org.json.JSONArray;
import org.json.JSONObject;
import yunhe.biz.ISaleBiz;
import yunhe.biz.impl.SaleBizImpl;
import yunhe.entity.Sale;
import yunhe.entity.SaleAppend;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lzh on 2017/6/15.
 */
@WebServlet(name = "GetPendingSal",urlPatterns ="/stockmgr/GetPendingSal")
public class GetPendingSal extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String salesId=request.getParameter("salesId");
        ISaleBiz ib=new SaleBizImpl();
        if (salesId!=null){
            List<SaleAppend> lsa=ib.saleListAndAppendAndProBySalesId(salesId);
            JSONArray ja=new JSONArray();
            for (SaleAppend s:lsa){
                JSONObject jo=new JSONObject(s);
                ja.put(jo);
            }
            //System.out.println(ja.toString());
            response.getWriter().print(ja.toString());
        }
        else{
            List<Sale> ls=ib.saleListUnExcute();

            JSONArray ja=new JSONArray();
            if(!ls.isEmpty()){
                for (Sale s:ls){
                    Map<String,String> mss=new HashMap<>();
                    mss.put("salesId",s.getSalesId());
                    mss.put("customerId",s.getCustomer().getCusName());
                    mss.put("saleDate",s.getSaleDate().toString());
                    mss.put("name",s.getEmpMsg().getName());
                    mss.put("accountReceiving",Double.toString(s.getAccountReceived()));
                    mss.put("accountReceived",Double.toString(s.getAccountReceived()));
                    mss.put("excuteSate","待执行");
                    ja.put(mss);
                }
                //System.out.println("{\"total\":1,\"rows\":"+ja.toString()+"}");
                response.getWriter().print("{\"total\":20,\"rows\":"+ja.toString()+"}");
            }
        }

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}